''' LineSense Modified to use lists and for loops ''' from botcore import * from time import sleep import random threshold = 2000 line_count = 0 beeps = [400, 500, 600, 700, 800, 900, 1000, 1100, 1200] patterns = [0b10101010, 0b01010101, 0b00111100, 0b00100100, 0b01000010, 0b10000001] # - Traverse patterns list and get random beeps def light_show(seconds, delay): num_lights = round(seconds/delay) for index in range(num_lights): freq = random.choice(beeps) lights = patterns[index % len(patterns)] leds.user(lights) spkr.pitch(freq) sleep(delay) spkr.off() # Turn off LEDs when finished leds.user(0) def detect_line(n): val = ls.read(n) is_detected = val < threshold leds.ls_num(n, is_detected) return is_detected # Modified to use a for loop def scan_lines(): got_line = False for n in range(5): if detect_line(n): got_line = True return got_line def go_forward(): motors.run(LEFT, 40) motors.run(RIGHT, 40) def back_turn(): motors.run(LEFT, -20) motors.run(RIGHT, -20) light_show(1.5, 0.3) motors.run(LEFT, 20) motors.run(RIGHT, -20) light_show(1.3, 0.1) # -- Main Program -- while True: if buttons.was_pressed(0): break motors.enable(True) while True: hit = scan_lines() if hit: back_turn() line_count = line_count + 1 if line_count > 255: line_count = 0 leds.user(line_count) else: go_forward()